package org.umss.fixturebuilder.controller;

import com.googlecode.objectify.Key;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.umss.fixturebuilder.builder.data.OfyService.ofy;
import org.umss.fixturebuilder.model.appengine.ChampionshipModel;
import org.umss.fixturebuilder.model.appengine.PhaseModel;
import org.umss.fixturebuilder.model.appengine.TeamModel;
import org.umss.fixturebuilder.model.json.JsonNewChampionship;
import org.umss.fixturebuilder.model.appengine.TeamPosition;
import org.umss.fixturebuilder.model.json.JsonScoreBoard;

/**
 *
 * @author Daniela Meneses
 */
public class ChampionshipController {

    public static boolean create(JsonNewChampionship championship) {
        ChampionshipModel created = new ChampionshipModel(championship.getName().replace(' ', '-'));
        if (!existWithoutPhases(championship.getName())) {
            Key<ChampionshipModel> championshipKey = ofy().save().entity(created).now();
            List<String> teamsNames = championship.getTeams();
            List<TeamModel> teamsToSave = new ArrayList<TeamModel>(teamsNames.size());
            List<TeamPosition> teamsPositionsToSave = new ArrayList<TeamPosition>(teamsNames.size());

            for (String teamName : teamsNames) {
                int frequency = Collections.frequency(teamsNames, teamName);
                if (frequency != 1) {
                    return false;
                }
                TeamModel team = new TeamModel(teamName, championshipKey);
                TeamPosition position = new TeamPosition(championshipKey, teamName);
                teamsToSave.add(team);
                teamsPositionsToSave.add(position);
            }

            ofy().save().entities(teamsToSave);
            ofy().save().entities(teamsPositionsToSave);
            return true;
        }
        
        return false;
    }

    public static boolean existWithoutPhases(String championshipId) {
        Key<ChampionshipModel> championshipKey = Key.create(ChampionshipModel.class, championshipId);
        ChampionshipModel now = ofy().load().type(ChampionshipModel.class)
                .filterKey(championshipKey).first().now();
        if (now != null) {
            return ofy().load().type(PhaseModel.class).ancestor(championshipKey).list().isEmpty();
        }
        return false;
    }

    public static List<String> getAll() {
        List<Key<ChampionshipModel>> list = ofy().load().type(ChampionshipModel.class).keys().list();
        List<String> championships = new ArrayList<String>();

        for (Key<ChampionshipModel> key : list) {
            String name = key.getName();
            championships.add(name);
        }

        return championships;
    }

    public static JsonScoreBoard getScoreBoard(String championshipId) {
        Key<ChampionshipModel> championshipKey = Key.create(ChampionshipModel.class, championshipId);
        List<TeamPosition> list = ofy().load().type(TeamPosition.class).ancestor(championshipKey).order("points").list();
        return new JsonScoreBoard(list);
    }
}
